package org.jeecg.modules.demo.shoprecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.customer.entity.BusCustomer;
import org.jeecg.modules.customer.service.IBusCustomerService;
import org.jeecg.modules.demo.shop.entity.VipShop;
import org.jeecg.modules.demo.shop.service.IVipShopItemService;
import org.jeecg.modules.demo.shop.service.IVipShopService;
import org.jeecg.modules.demo.shoprecord.entity.ShopRecord;
import org.jeecg.modules.demo.shoprecord.mapper.ShopRecordMapper;
import org.jeecg.modules.demo.shoprecord.service.IShopRecordService;
import org.jeecg.modules.demo.totalpoints.entity.BusVipPoint;
import org.jeecg.modules.demo.totalpoints.service.IBusVipPointService;
import org.jeecg.modules.message.wechat.FuWuHaoMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 兑换记录
 * @Author: jeecg-boot
 * @Date: 2021-07-30
 * @Version: V1.0
 */
@Service
public class ShopRecordServiceImpl extends ServiceImpl<ShopRecordMapper, ShopRecord> implements IShopRecordService {

    @Autowired
    private IBusVipPointService vipPointService;

    @Autowired
    private IVipShopService shopService;

    @Autowired
    private IBusCustomerService customerService;

    @Autowired
    private FuWuHaoMessageService fuWuHaoMessageService;

    @Override
    public Page<ShopRecord> queryRecordList(Page<ShopRecord> page, QueryWrapper<ShopRecord> ew) {
        return this.baseMapper.queryRecordList(page, ew);
    }

    /**
     * 兑换核销积分
     *
     * @param shopRecord
     */
    @Override
    public Result saveWithPoints(ShopRecord shopRecord) {
        String userId = shopRecord.getUserId();

        if (StringUtils.isBlank(userId)) {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            userId = user.getId();
            shopRecord.setUserName(user.getRealname());
        }
        LambdaQueryWrapper<BusCustomer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerLambdaQueryWrapper.eq(BusCustomer::getSysUserId, userId);
        BusCustomer customer = customerService.getOne(customerLambdaQueryWrapper);
        VipShop shopPro = shopService.getById(shopRecord.getCommodityId());
        Integer usedPoints = shopRecord.getComNum() * shopPro.getUsePoints();
        shopRecord.setCusId(customer.getId());

        LambdaQueryWrapper<BusVipPoint> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BusVipPoint::getUserId, userId);
        BusVipPoint vipPoint = vipPointService.getOne(wrapper);
        if (vipPoint.getUseablePoints() < usedPoints) {
            return Result.error(500,"积分不足");
        }
        vipPoint.setPoints(vipPoint.getPoints() - usedPoints);
        vipPoint.setUseablePoints(vipPoint.getUseablePoints() - usedPoints);
        vipPointService.updateById(vipPoint);
        shopRecord.setUserId(userId);
        shopRecord.setPointsNum(usedPoints);
        shopRecord.setComName(shopPro.getName());
        shopRecord.setCommPoints(shopPro.getUsePoints());
        save(shopRecord);

        fuWuHaoMessageService.SendCusPointsExchangeWxMsg(shopRecord, shopPro, customer);
        return Result.OK("请求成功");
    }
}
